﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Ribbon;
using t01crm.View;
using t01crm.Data;
using System.Runtime.InteropServices;


using t01crm.Business;

namespace t01crm
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            //InitializeCheckBoxes();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            ViewManager vm = ViewManager.Create(this.MainContent, true);


            this.DataContext = this.ViewModel;

            //removed:
            //FrameworkElement fe = ViewManager.Current.GetView(Views.Profile);
            //MainViewModel mv = this.DataContext as MainViewModel;
            //mv.CurrentProfile = new Profile();            
            //fe.DataContext = this.DataContext;
            //ViewManager.Current.Transition(fe);


        }

        protected MainViewModel ViewModel = new MainViewModel();

        private void RibbonCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void RibbonCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {

            RibbonCommand rc = (RibbonCommand)e.Command;

            FrameworkElement fe = null;
           

           
            switch (rc.LabelTitle)
            {
                case "压缩内存":
                    FlushMemory();
                    break;
                case "销售清单":
                    fe = ViewManager.Current.GetView(Views.SalesList);
                    break;
                case "分类维护":
                    fe = ViewManager.Current.GetView(Views.Catalog);
                    break;
                default:
                    break;
            }
            if (fe != null)
            {
                MainViewModel mv = this.DataContext as MainViewModel;
                fe.DataContext = this.DataContext;
                ViewManager.Current.Transition(fe);
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DetailsWindow details = new DetailsWindow();
            details.ShowDialog();
        }

        private void ribbonApplicationMenu_MostRecentFileSelected(object sender, MostRecentFileSelectedEventArgs e)
        {
            MessageBox.Show(((MostRecentFile)e.SelectedItem).Name, "Most Recent File Selected:");
        }



        private void RibbonCheckBox_Checked(object sender, RoutedEventArgs e)
        {

            if ((RibbonCheckBox)sender == TitleCheckBox)
            {

            }
            ViewManager.Current.Transition(ViewManager.Current.GetView(Views.Map));
            MapContextualGroup.IsActive = true;
        }

        private void RibbonCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if ((RibbonCheckBox)sender == TitleCheckBox)
            {

            }
            ViewManager.Current.Transition(ViewManager.Current.BackView);
            MapContextualGroup.IsActive = false;

        }



        private void RibbonCommand_NewProfile_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            FrameworkElement fe = ViewManager.Current.GetView(Views.Profile);
            ProfileContextualGroup.IsActive = true;
            ClientProfile.IsSelected = true;

            if (fe != null)
            {
                MainViewModel mv = this.DataContext as MainViewModel;


                //mv.CurrentProfile = new Profile();
                //mv.CurrentProfile.FirstName = "Windows";
                //mv.CurrentProfile.LastName = "Presentation Foundation";
                //mv.CurrentProfile.HomePhoneNo = "123-1234";
                //mv.CurrentProfile.MobilePhoneNo = "123-1234";

                //mv.CurrentProfile.Address = new Address();
                //mv.CurrentProfile.Address.Address1 = "One Microsoft Way";
                //mv.CurrentProfile.Address.Address2 = "Bldg 20";
                //mv.CurrentProfile.Address.State = "WA";
                //mv.CurrentProfile.Address.City = "Redmond";
                //mv.CurrentProfile.Address.ZipCode = "98052";

                //mv.CurrentProfile.Preference = new Preference();
                //mv.CurrentProfile.Preference.Acreage = 8000;
                //mv.CurrentProfile.Preference.PriceStart = 200000;
                //mv.CurrentProfile.Preference.PriceStop = 300000;
                //mv.CurrentProfile.Preference.MinBaths = 2;
                //mv.CurrentProfile.Preference.HasView = true;
                //mv.CurrentProfile.Preference.HasWaterFront = true;
                //mv.CurrentProfile.Preference.MinGarage = 4;
                //mv.CurrentProfile.Preference.MinRooms = 5;
                //mv.CurrentProfile.Preference.SquareFeet = 2000;
                //mv.CurrentProfile.Preference.YearsOld = 1950;


                fe.DataContext = this.DataContext;
                ViewManager.Current.Transition(fe);
            }
        }

        private void HackToUpdateView(object sender, RoutedEventArgs e)
        {
            RibbonTab t = sender as RibbonTab;


            if (t.Name == "SearchTab" || t.Name == "SearchResults")
            {
                //ViewManager.Current.Transition ( ViewManager.Current.GetView (Views.Search));
                MapContextualGroup.IsActive = false;
                ProfileContextualGroup.IsActive = false;
            }
        }










        [DllImport("kernel32.dll")]

        public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);

        public void FlushMemory()
        {

            GC.Collect();

            GC.WaitForPendingFinalizers();

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {

                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);

            }

        }

    }


}
